<!-- 个人中心 -->
<template>
    <el-card>
        <template #header>
            <div class="card-header">普通读者用户中心</div>
        </template>
        <div class="card-content">
            <el-row>
                <el-col :span="12">
                    <el-card>
                        <template #header>个人信息</template>
                        <div>
                            <p>姓名：[读者姓名]</p>
                            <p>读者编号：[读者编号]</p>
                            <p>注册日期：[注册日期]</p>
                        </div>
                    </el-card>
                </el-col>
                <el-col :span="12">
                    <el-card>
                        <template #header>借阅信息</template>
                        <div>
                            <p>已借阅图书数量：[已借阅数量]</p>
                            <p>可借阅图书数量：[可借阅数量]</p>
                            <p>逾期未还图书数量：[逾期数量]</p>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            <el-row style="margin-top: 20px;">
                <el-col :span="24">
                    <el-card>
                        <template #header>借阅记录</template>
                        <el-table :data="borrowRecords" stripe>
                            <el-table-column prop="bookName" label="图书名称"></el-table-column>
                            <el-table-column prop="borrowDate" label="借阅日期"></el-table-column>
                            <el-table-column prop="returnDate" label="应还日期"></el-table-column>
                            <el-table-column prop="dueDate" label="实际归还日期"></el-table-column>
                        </el-table>
                    </el-card>
                </el-col>
            </el-row>
        </div>
    </el-card>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { useUserStore } from '@/stores/user'
import api from '@/api'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

const userStore = useUserStore()
const borrowRecords = ref([])
const loading = ref(false)

// 日期格式化方法
const formatDate = (dateStr) => {
    return dateStr ? dayjs(dateStr).format('YYYY-MM-DD') : '未知日期'
}

// 获取借阅记录
const fetchBorrowRecords = async () => {
    try {
        loading.value = true
        const res = await api.book.userBorrowList({
            userId: userStore.library_users.userId
        })
        borrowRecords.value = res.data.map(item => ({
            bookName: item.bookTitle,
            borrowDate: formatDate(item.borrowDate),
            returnDate: formatDate(item.dueDate),
            actualReturnDate: formatDate(item.returnDate)
        }))
    } catch (error) {
        ElMessage.error('获取借阅记录失败')
        console.error('API Error:', error)
    } finally {
        loading.value = false
    }
}

onMounted(() => {
    if (userStore.library_users?.userId) {
        fetchBorrowRecords()
    }
})
</script>

<style scoped>
.card-header {
    font-size: 18px;
    font-weight: bold;
}

.card-content {
    padding: 20px;
}
</style>
